<?php

    namespace Model;

    class eventsModel {
        private $DB;

        public function __construct() {
            $this->DB = mysqli_connect('kvd_mysql', 'root', 'root', 'kvd');
        }

        public function addEvent($admin_id, $title, $description, $event_data, $place) {
            $admin_id = (integer) $admin_id;
            $data = date("Y.m.d");
            $sql = "INSERT INTO events (admin_id ,title, description, event_data, place, status,created, updated)
                            VALUES
                            ($admin_id , '$title', '$description', '$event_data', '$place', 1,'$data', '$data')";
            $response = $this->DB->query($sql);
            $sql = "select * from events where title = '$title' and admin_id = $admin_id and description = '$description'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => "1",
                ];
            } else {
                return [
                    "status" => "0"
                ];
            }
        }

        public function getAllEvents() {
            $sql = "select * from kvd.events where 1;";
            $sql = $this->DB->real_escape_string($sql);
            $response = $this->DB->query($sql)->fetch_all(MYSQLI_ASSOC);
            if ($response != null) {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function deleteEvent($admin_id, $event_id) {
            $sql = "select * from kvd.events where admin_id = $admin_id and id = $event_id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response == null){
                return [
                    "status" => 0
                ];
            }
            $sql = "delete from events where admin_id = $admin_id and id = $event_id";
            $response = $this->DB->query($sql);
            $sql = "select * from kvd.events where admin_id = $admin_id and id = $event_id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response == null){
                return [
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function getEventById($id = null) {
            if (!is_integer($id)) {
                return [
                    "status" => 0
                ];
            }
            $sql = "select * from events where id = '$id'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }
    }